/*
 *  Copyright (C) 2011  social-tourist.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.socialtourist.web.client.widgets.pages;

import java.util.List;

import org.socialtourist.web.client.ContentContainer;
import org.socialtourist.web.client.SocialTourist;
import org.socialtourist.web.client.SocialTouristServiceAsync;
import org.socialtourist.web.client.widgets.MapMark;
import org.socialtourist.web.client.widgets.MapMarkClickHandler;
import org.socialtourist.web.client.widgets.ProjectDetailsPanel;
import org.socialtourist.web.client.widgets.SocialTouristContent;
import org.socialtourist.web.shared.data.SocialProject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class PageMap extends SocialTouristContent implements MapMarkClickHandler, AsyncCallback<List<SocialProject>> {
	
	private SocialTouristServiceAsync service;
	private ProjectDetailsPanel details;
	private AbsolutePanel panel;
	private Image image;


	public PageMap(ContentContainer container, SocialTouristServiceAsync service) {
		super(container);
	
		this.service = service;
		this.details = new ProjectDetailsPanel(service);
		image = new Image(SocialTourist.imageBundle.bgMap800());
		
//		VerticalPanel panel = new VerticalPanel();
//		panel.setSize("800", "600");
//		panel.add(image);
//		
//		Button button = new Button("X");
//		panel.add(button);
		
		panel = new AbsolutePanel();
		panel.setSize("800px", "700px");
		panel.add(image);
		
		
		
		initWidget(panel);
	}
	
	

	private void initMarks(List<SocialProject> projects, AbsolutePanel panel) {
		for (SocialProject sp : projects) {
			MapMark mark = new MapMark(sp);
			mark.addMapMarkClickHandler(this);
			panel.add(mark, sp.getX(), sp.getY());			
		}
		
	}



	@Override
	public void onMapMarkClick(MapMark mark, ClickEvent event) 
	{
		details.setProject(mark.getProject());
		details.center();		
	}



	@Override
	public void onFailure(Throwable caught) {
		panel.add(new Label("Error initializing: " + caught.getMessage()));
		Label box = new Label();
		StringBuffer text = new StringBuffer();
		text.append(caught.getLocalizedMessage());
		for (StackTraceElement element : caught.getStackTrace()) 
		{
			text.append(element.toString());
			text.append("<br>");
		}
		box.setText(text.toString());
		panel.add(box);
		
	}



	@Override
	public void onSuccess(List<SocialProject> projects) {
		assert (projects != null && projects.size() != 0);	
		initMarks(projects, panel);
		
	}
}
